package com.leather.user.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.leather.common.entity.User;
import com.leather.user.vo.UserVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author kevin
 */
public interface UserMapper extends BaseMapper<User> {

    @Select("select number,nick_name nickName,description,img_url imgUrl from user where id = #{userId}")
    UserVo getUserVoById(@Param("userId") int userId);

    /**
     * 统计获取验证码的历史
     *
     * @return
     */
    @Select("select count(*) from history ${ew.customSqlSegment}")
    int countHistory(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 插入获取验证码记录
     *
     * @param number
     * @param year
     * @param month
     * @return
     */
    @Insert("insert into history(number,year,month) values(#{number},#{year},#{month})")
    int insertHistory(@Param("number") String number, @Param("year") int year, @Param("month") int month);

    /**
     * 获取关注数
     *
     * @param userId
     * @return
     */
    @Select("select count(*) from touch where user_id = #{userId}")
    int selectTouchCount(@Param("userId") int userId);

    /**
     * 获取粉丝数
     *
     * @param userId
     * @return
     */
    @Select("select count(*) from touch where touch_id = #{userId}")
    int selectTouchedCount(@Param("userId") int userId);

    /**
     * 获取用户的关注数
     * @return
     */
    @Select("select  a.id,a.nick_name,a.img_url from user a,touch b where b.user_id =#{userId} and a.id = b.touch_id")
    List<UserVo> getTouchList(@Param("userId") int  userId);

    /**
     * 获取用户的粉丝
     * @return
     */
    @Select("select  a.id,a.nick_name,a.img_url from user a,touch b where b.touch_id =#{userId} and a.id = b.user_id")
    List<UserVo> getStar(@Param("userId") int userId);

    @Select("select count(*) from touch where user_id = #{nowId} and touch_id = #{userId}")
    int getIsTouched(@Param("nowId") int nowId, @Param("userId") int userId);

    @Insert("insert into touch(user_id,touch_id,create_time) values(#{userId},#{touchId},now())")
    int touchUser(@Param("userId") int userId, @Param("touchId") int touchId);

    @Delete("delete from touch where user_id = #{userId} and touch_id = #{touchId}")
    int deleteTouchUser(@Param("userId") int userId, @Param("touchId") int touchId);

}